package com.juzipi.demo.web;


import com.juzipi.demo.pojo.Blog;
import com.juzipi.demo.service.BlogService;
import com.juzipi.demo.service.TagService;
import com.juzipi.demo.service.TypeService;
import com.juzipi.demo.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class IndexController {


    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;


    @GetMapping("/")
    public String index(@PageableDefault(size = 6,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, Blog blog, Model model) {
        model.addAttribute("page",blogService.listBlog(pageable));
        model.addAttribute("types",typeService.listTypeTop(10));
        model.addAttribute("tags",tagService.listTagTop(20));
        model.addAttribute("recommendBlogs",blogService.listRecommendBlogTop(6));
        return "index";
    }

    @PostMapping("/search")
    public String search(@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, @RequestParam String query, Model model) {

        model.addAttribute("page",blogService.listBlog("%"+query+"%",pageable));
        model.addAttribute("query",query);

        return "search";
    }



    @GetMapping("/blog/{id}")
    public String blog(@PathVariable Long id, Model model) {
        model.addAttribute("blog",blogService.getAndConvert(id));

        return "blog";
    }


    @GetMapping("/footer/newblog")
    public String newBlog(Model model){

        model.addAttribute("newblogs",blogService.listRecommendBlogTop(3));
        return "_fragments :: newblogList";
    }

//    @GetMapping("/archives")
//    public String archives() {
//        System.out.println("------------index----------");
//        return "archives";
//    }

//    @GetMapping("/types")
//    public String types() {
//        System.out.println("------------index----------");
//        return "types";
//    }
//
//    @GetMapping("/about")
//    public String about() {
//        System.out.println("------------index----------");
//        return "about";
//    }

}
